#ifndef COCOSBUTTON_H_20111115
#define COCOSBUTTON_H_20111115

#include "cocos2d.h"

using namespace cocos2d;

class CButtonListener
{
public:
	virtual void onButtonClick(int aID) = 0;
};

class CSpriteButton : public cocos2d::CCSprite, public cocos2d::CCStandardTouchDelegate
{
public:
	void onEnter()
	{
		cocos2d::CCSprite::onEnter();
		cocos2d::CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);
	}

	void onExit()
	{
		cocos2d::CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
		cocos2d::CCSprite::onExit();
	}

	bool containsTouchLocation(cocos2d::CCTouch *pTouch)
	{
		cocos2d::CCSize s = getContentSize(); 

		return (CCRect::CCRectContainsPoint(CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height),convertTouchToNodeSpaceAR(pTouch)));
	}

public:
	static CSpriteButton* spriteWithTexture(cocos2d::CCTexture2D *pTexture)
	{
		CSpriteButton *pobSprite = new CSpriteButton();
		pobSprite->initWithTexture(pTexture);
		pobSprite->autorelease();

		return pobSprite;
	}

	static CSpriteButton* spriteWithTexture(cocos2d::CCTexture2D *pTexture, cocos2d::CCRect rect)
	{
		CSpriteButton *pobSprite = new CSpriteButton();
		pobSprite->initWithTexture(pTexture, rect);
		pobSprite->autorelease();

		return pobSprite;
	}


	static CSpriteButton* spriteWithFile(const char *pszFileName)
	{
		CSpriteButton *pobSprite = new CSpriteButton();
		pobSprite->initWithFile(pszFileName);
		pobSprite->autorelease();

		return pobSprite;
	}

	static CSpriteButton* spriteWithFile(const char *pszFileName, cocos2d::CCRect rect)
	{
		CSpriteButton *pobSprite = new CSpriteButton();
		pobSprite->initWithFile(pszFileName, rect);
		pobSprite->autorelease();

		return pobSprite;
	}

	static CSpriteButton* spriteWithSpriteFrame(cocos2d::CCSpriteFrame *pSpriteFrame)
	{
		CSpriteButton *pobSprite = new CSpriteButton();
		pobSprite->initWithSpriteFrame(pSpriteFrame);
		pobSprite->autorelease();

		return pobSprite;
	}

	static CSpriteButton* spriteWithSpriteFrameName(const char *pszSpriteFrameName)
	{
		cocos2d::CCSpriteFrame *pFrame = cocos2d::CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pszSpriteFrameName);
		return spriteWithSpriteFrame(pFrame);
	}

public:
	virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) 
	{
		if(!m_bEnable)
		{
			return;
		}
		if(!pTouches||!pTouches->count())
			return;

		CCSetIterator iter;
		for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
		{
			if(containsTouchLocation((CCTouch *)(*iter)))
			{
				m_pCurTouch = (CCTouch *)(*iter);

				m_bTouched = true;
				if(TYPE_SCALE == m_type)
				{
					setScale(1.1f);
				}

			}
		}
		
	}

	virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) 
	{
		if(!m_bEnable)
		{
			return;
		}
		if(!pTouches||!pTouches->count())
			return;

		CCSetIterator iter;
		for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
		{
			if(*iter == m_pCurTouch)
			{
				if(!containsTouchLocation((CCTouch *)(*iter)))
				{
					setScale(1.0f);
					m_bTouched = false;
					m_pCurTouch = NULL;
				}
			}
		}
	}

	virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) 
	{
		if(!m_bEnable)
		{
			return;
		}
		if(!pTouches||!pTouches->count())
			return;

		CCSetIterator iter;
		for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
		{
			if(*iter == m_pCurTouch)
			{
				if(TYPE_SCALE == m_type)
				{
					setScale(1.0f);
				}

				if(m_bTouched)
				{
					if(NULL != m_pListener)
					{
						m_pListener->onButtonClick(m_id);
					}

					m_bTouched = false;
				}
			}
		}
		
	}

	virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) 
	{
		if(!m_bEnable)
		{
			return;
		}
		if(!pTouches||!pTouches->count())
			return;

		CCSetIterator iter;
		for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
		{
			if(*iter == m_pCurTouch)
			{
				if(TYPE_SCALE == m_type)
				{
					setScale(1.0f);
				}

				if(m_bTouched)
				{
					if(NULL != m_pListener)
					{
						m_pListener->onButtonClick(m_id);
					}

					m_bTouched = false;
				}
			}
		}
		
	}

public:
	enum ButtonType
	{
		TYPE_SCALE,
		TYPE_NONE,
		TYPE_TRANSPARENT,
	};
public:
	CSpriteButton()
		: m_pListener(NULL)
		, m_bEnable(false)
		, m_bTouched(false)
	{

	}
	CSpriteButton(CButtonListener *pListener)
		: m_pListener(pListener)
	{

	}
	~CSpriteButton()
	{

	}

public:
	void SetID(int id) {m_id = id;}
	int  GetID() const {return m_id;}
	void SetData(int id, CButtonListener *pListerner, ButtonType type = TYPE_SCALE) 
	{
		m_id = id; 
		m_pListener = pListerner;
		m_type = type;
	}

	void SetIsEnable(bool bEnable) {m_bEnable = bEnable;}
	void Reset()
	{
		if(TYPE_SCALE == m_type)
		{
			setScale(1.0f);
		}
	}
private:
	CButtonListener *m_pListener;
	int              m_id;
	bool             m_bEnable;
	ButtonType       m_type;
	bool             m_bTouched;
	cocos2d::CCTouch *m_pCurTouch;
};

#endif